# GPL3 or any later version

import os
import json

def save_folder(fold="jyt/"):

    # gets the save / settings folder
    
    try:
        data_dir = os.environ["XDG_DATA_HOME"] + "/" + fold 
    except:
        data_dir = os.path.expanduser("~/.local/share/"+fold)

    try:
        os.makedirs(data_dir)
    except:
        pass

    return data_dir

def get_list():

    # gets list of the wallets

    folder = save_folder()+"wallets/"
    try:
        os.makedirs(folder)
    except:
        pass

    wallets = list(os.walk(folder))[0][1]
    wallets = sorted(wallets)
    return wallets
    
def get_totals(the_wallet):

    # returns wallet data

    try:
        with open(save_folder()+"wallets/"+the_wallet+"/totals.json") as json_file: 
            data = json.load(json_file)
    except Exception as e:
        data = {}

    return data

def save_totals(the_wallet, data):

    print("saving")
    
    with open(save_folder()+"wallets/"+the_wallet+"/totals.json", "w") as fp:
        json.dump(data, fp, indent=4)

